package com.efounder.frame.manager;

import android.app.Activity;
import android.util.Log;

import java.lang.ref.WeakReference;
import java.util.Stack;

/**
 * Created by hudq on 2017/1/18.
 */

public class EFAppAccountActivityManager {
    private static Stack<WeakReference<Activity>> activityStack;

    public static Activity push(Activity activity){
        activityStack.push(new WeakReference<Activity>(activity));
        return activity;
    }

    public static Activity pop(){
        WeakReference<Activity> weakReferenceActivity = activityStack.pop();
        Activity activity = weakReferenceActivity.get();
        if (activity != null && activity.isFinishing())
            activity.onBackPressed();
        return activity;
    }

    public static void popAll(){
        int size = activityStack.size();
        for (int i=0; i < size; i++){
            WeakReference<Activity> weakReferenceActivity = activityStack.pop();
            Activity activity = weakReferenceActivity.get();
            if (activity != null && activity.isFinishing())
                activity.onBackPressed();
        }
    }

    public static Activity peek(){
        WeakReference<Activity> weakReferenceActivity = activityStack.peek();
        return weakReferenceActivity.get();
    }

    public static int size(){
        return activityStack.size();
    }

}
